package com.afdor.rws.dtm.transaction;

import java.util.Arrays;
import java.util.Objects;

/**
 * 事务状态
 *
 * @author 悭梵
 * @date Created in 2018-10-12 17:45
 */
public enum Status {
    /**
     * Framework has been created transaction, But it hasn't started yet.
     */
    CREATE(0, "新建"),
    /**
     * Complete transaction and wait for commit
     */
    PRE_COMMIT(1, "预提交"),
    /**
     * Commit transaction
     */
    COMMIT(2, " 提交"),
    /**
     * Rollback transaction
     */
    ROLLBACK(3, " 回滚"),
    /**
     * Running transaction
     */
    RUNNING(4, "执行中"),
    /**
     * 成功
     */
    SUCCESS(5, "成功"),
    /**
     * 失败，待重试
     */
    FAILURE(6, "失败"),
    /**
     * 已终止
     */
    TERMINATED(7, "终止"),
    /**
     * 已放弃
     */
    ABANDONED(8, "放弃"),
    /**
     * 部分完成
     */
    PART_SUCCESS(9, "部分成功"),
    ;


    private final int value;

    private final String label;

    Status(int value, String label) {
        this.label = label;
        this.value = value;
    }

    public int value() {
        return this.value;
    }

    public String label() {
        return this.label;
    }

    public static Status acquireByValue(final int value) {
        return Arrays.stream(Status.values())
                .filter(v -> Objects.equals(v.value(), value))
                .findFirst()
                .orElse(Status.CREATE);
    }

    public static String acquireLabelByValue(final int value) {
        return acquireByValue(value).label();
    }

}
